﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using KC.Common;
using KC.Framework.Tenant;
using KC.Service.Core.Base;
using KC.Service.Core.Constants;
using KC.DTO.Account;
using KC.DTO.Core;
using KC.Service.Core.DTO.Search;
using KC.Framework.Base;

namespace KC.Service.Core.WebApiService.Business
{
    public class AccountApiService : IdSrvOAuth2ClientRequestBase, IAccountApiService
    {
        private const string ServiceName = "KC.Service.Core.WebApiService.AccountApiService";

        public AccountApiService(
            Tenant tenant, 
            System.Net.Http.IHttpClientFactory httpClient,
            Microsoft.Extensions.Logging.ILogger<AccountApiService> logger)
            : base(tenant ?? TenantConstant.DbaTenantApiAccessInfo, httpClient, logger)
        {
        }

        #region 登录（包括：微信）
        /// <summary>
        /// 获取用户登录信息
        /// </summary>
        /// <param name="userName">用户名/用户邮箱/用户手机</param>
        /// <param name="password">用户密码</param>
        /// <returns></returns>
        public async Task<ServiceResult<UserDTO>> TenantUserLogin(string userName, string password)
        {
            ServiceResult<UserDTO> result = null;
            await WebSendGetAsync<ServiceResult<UserDTO>>(
                ServiceName + ".TenantUserLogin",
                AccoutApiServerUrl + "AccountApi/TenantUserLogin?userName=" + userName + "&password=" + password,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<UserDTO>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            return result;
        }
        /// <summary>
        /// 更新登录用户的OpenId
        /// </summary>
        /// <param name="userId">用户Guid</param>
        /// <param name="openId">用户的微信OpenId</param>
        /// <returns></returns>
        public async Task<ServiceResult<string>> UpdateUserWxOpenId(string userId, string openId)
        {
            ServiceResult<string> result = null;
            await WebSendGetAsync<ServiceResult<string>>(
                ServiceName + ".UpdateUserWxOpenId",
                AccoutApiServerUrl + "AccountApi/UpdateUserWxOpenId?userId=" + userId + "&openId=" + openId,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<string>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            return result;
        }
        #endregion

        #region 开通服务
        public async Task<ServiceResult<bool>> OpenEmailService(EmailConfig config)
        {
            var postData = SerializeHelper.ToJson(config);
            ServiceResult<bool> result = null;
            await WebSendPostAsync<ServiceResult<bool>>(
                ServiceName + ".OpenEmailService",
                AccoutApiServerUrl + "AccountApi/OpenEmailService",
                postData,
                ApplicationConstant.AccScope, 
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                false);

            return result;
        }

        public async Task<ServiceResult<bool>> OpenSmsService(SmsConfig config)
        {
            var postData = SerializeHelper.ToJson(config);
            ServiceResult<bool> result = null;
            await WebSendPostAsync<ServiceResult<bool>>(
                ServiceName + ".OpenSmsService",
                AccoutApiServerUrl + "AccountApi/OpenSmsService",
                ApplicationConstant.AccScope,
                postData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                false);

            return result;
        }

        public async Task<ServiceResult<bool>> OpenCallCenterService(CallConfig config)
        {
            var postData = SerializeHelper.ToJson(config);
            ServiceResult<bool> result = null;
            await WebSendPostAsync<ServiceResult<bool>>(
                ServiceName + ".OpenCallCenterService",
                AccoutApiServerUrl + "AccountApi/OpenCallCenterService",
                ApplicationConstant.AccScope,
                postData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                false);

            return result;
        }

        public async Task<bool> GetIsEnabledWorkFlow(Guid id)
        {
            ServiceResult<bool> result = null;
            await WebSendGetAsync<ServiceResult<bool>>(
                ServiceName + ".GetIsEnabledWorkFlow",
                AccoutApiServerUrl + "AccountApi/GetIsEnabledWorkFlow?id=" + id,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                false);

            return result.success && result.Result;
        }
        #endregion

        #region 角色信息
        public async Task<List<RoleSimpleDTO>> GetAllSimpleRoles(TenantType? TenantType = null)
        {
            var key = TenantType.HasValue
                ? "com-KC.Service.Core.WebApiService.Business.IAccountApiService-GetAllSimpleRoles-" +
                  TenantType.Value
                : "com-KC.Service.Core.WebApiService.Business.IAccountApiService-GetAllSimpleRoles";
            var allPermissions = KC.Service.Core.Util.LocalWebCacheUtil.GetCache<List<RoleSimpleDTO>>(key);
            if (allPermissions != null) return allPermissions;

            ServiceResult<List<RoleSimpleDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<RoleSimpleDTO>>>(
                ServiceName + ".GetAllSimpleRoles",
                AccoutApiServerUrl + "AccountApi/GetAllSimpleRoles?TenantType=" + TenantType,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<RoleSimpleDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                KC.Service.Core.Util.LocalWebCacheUtil.SetCache(key, result.Result, TimeSpan.FromMinutes(TimeOutConstants.DefaultCacheTimeOut));
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取角色Id列表下的所有权限
        /// </summary>
        /// <param name="roleIds">角色Id列表</param>
        /// <returns></returns>
        public async Task<List<MenuNodeSimpleDTO>> GetUserMenusByRoleIds(List<string> roleIds)
        {
            var jsonData = SerializeHelper.ToJson(roleIds);
            ServiceResult<List<MenuNodeSimpleDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<MenuNodeSimpleDTO>>>(
                ServiceName + ".GetUserMenusByRoleIds",
                AccoutApiServerUrl + "AccountApi/GetUserMenusByRoleIds",
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<MenuNodeSimpleDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取角色Id列表下的所有权限
        /// </summary>
        /// <param name="roleIds">角色Id列表</param>
        /// <returns></returns>
        public async Task<List<PermissionSimpleDTO>> GetUserPermissionsByRoleIds(List<string> roleIds)
        {
            var jsonData = SerializeHelper.ToJson(roleIds);
            ServiceResult<List<PermissionSimpleDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<PermissionSimpleDTO>>>(
                ServiceName + ".GetUserPermissionsByRoleIds",
                AccoutApiServerUrl + "AccountApi/GetUserPermissionsByRoleIds",
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<PermissionSimpleDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取所有的权限
        /// </summary>
        /// <param name="TenantType"></param>
        /// <returns></returns>
        public async Task<List<PermissionSimpleDTO>> GetAllSimplePermissions(TenantType? TenantType = null)
        {
            var key = TenantType.HasValue
                ? "com-KC.Service.Core.WebApiService.Business.IAccountApiService-GetAllSimplePermissions-" +
                  TenantType.Value
                : "com-KC.Service.Core.WebApiService.Business.IAccountApiService-GetAllSimplePermissions";
            var allPermissions = KC.Service.Core.Util.LocalWebCacheUtil.GetCache<List<PermissionSimpleDTO>>(key);
            if (allPermissions != null) return allPermissions;

            ServiceResult<List<PermissionSimpleDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<PermissionSimpleDTO>>>(
                ServiceName + ".GetAllSimplePermissions",
                AccoutApiServerUrl + "AccountApi/GetAllSimplePermissions?TenantType=" + TenantType,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<PermissionSimpleDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                KC.Service.Core.Util.LocalWebCacheUtil.SetCache(key, result.Result, TimeSpan.FromMinutes(TimeOutConstants.DefaultCacheTimeOut));
                return result.Result;
            }

            return null;
        }


        /// <summary>
        /// 根据角色名称列表，获取角色列表（包含所有角色的用户）
        /// </summary>
        /// <param name="roleNames">角色名称列表</param>
        /// <returns></returns>
        public async Task<List<RoleDTO>> GetRolesWithUsersByNames(List<string> roleNames)
        {
            var jsonData = SerializeHelper.ToJson(roleNames);
            ServiceResult<List<RoleDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<RoleDTO>>>(
                ServiceName + ".GetRolesWithUsersByNames",
                AccoutApiServerUrl + "AccountApi/GetRolesWithUsersByNames",
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<RoleDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取角色的分页列表
        /// </summary>
        /// <param name="name">角色名</param>
        /// <param name="appId">应用Id</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页大小</param>
        /// <returns></returns>
        public async Task<PaginatedBaseDTO<RoleDTO>> GetPagenatedRolesByName(string name, string appId, int pageIndex, int pageSize)
        {
            ServiceResult<PaginatedBaseDTO<RoleDTO>> result = null;
            await WebSendGetAsync<ServiceResult<PaginatedBaseDTO<RoleDTO>>>(
                ServiceName + ".GetPagenatedRolesByName",
                AccoutApiServerUrl + "AccountApi/GetPagenatedRolesByName?name=" + name + "&appId=" + appId + "&pageIndex=" + pageIndex + "&pageSize=" + pageSize,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<PaginatedBaseDTO<RoleDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 根据角色名称，获取角色信息（包含所有角色的用户）
        /// </summary>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        public async Task<RoleDTO> GetRoleWithUsersByName(string roleName)
        {
            ServiceResult<RoleDTO> result = null;
            await WebSendGetAsync<ServiceResult<RoleDTO>>(
                ServiceName + ".GetRoleWithUsersByName",
                AccoutApiServerUrl + "AccountApi/GetRoleWithUsersByName?roleName=" + roleName,
                ApplicationConstant.AccScope, 
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<RoleDTO>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }

        /// <summary>
        /// 保存应用的权限数据
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public async Task<bool> SavePermissionsAsync(List<PermissionDTO> permissions, Guid appGuid)
        {
            var jsonData = SerializeHelper.ToJson(permissions);
            ServiceResult<bool> result = null;
            await WebSendPostAsync<ServiceResult<bool>>(
                ServiceName + ".SavePermissions",
                AccoutApiServerUrl + "AccountApi/SavePermissions?appId=" + appGuid,
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success)
            {
                return result.Result;
            }

            return false;
        }

        /// <summary>
        /// 保存应用的权限数据
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        public async Task<bool> SaveMenusAsync(List<MenuNodeDTO> menus, Guid appGuid)
        {
            var jsonData = SerializeHelper.ToJson(menus);
            ServiceResult<bool> result = null;
            await WebSendPostAsync<ServiceResult<bool>>(
                ServiceName + ".SaveMenusAsync",
                AccoutApiServerUrl + "AccountApi/SaveMenus?appId=" + appGuid,
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success)
            {
                return result.Result;
            }

            return false;
        }
        #endregion

        #region 租户内部的员工信息
        #region 获取租户员工信息
        /// <summary>
        /// 获取所有员工的简单信息
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetAllSimpleUsers()
        {
            ServiceResult<List<UserDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<UserDTO>>>(
                ServiceName + ".GetAllSimpleUsers",
                AccoutApiServerUrl + "AccountApi/GetAllSimpleUsers",
                ApplicationConstant.AccScope, 
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 根据用户Id列表，获取所有员工
        /// </summary>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetAllUsersWithRolesAndOrgs()
        {
            ServiceResult<List<UserDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<UserDTO>>>(
                ServiceName + ".GetAllUsersWithRolesAndOrgs",
                AccoutApiServerUrl + "AccountApi/GetAllUsersWithRolesAndOrgs",
                ApplicationConstant.AccScope, 
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取该组织下的所有员工
        /// </summary>
        /// <param name="orgId">组织Id</param>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetUsersByOrgId(int orgId)
        {
            ServiceResult<List<UserDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<UserDTO>>>(
                ServiceName + ".GetUsersByOrgId",
                AccoutApiServerUrl + "AccountApi/GetUsersByOrgId?orgId=" + orgId,
                ApplicationConstant.AccScope, 
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 根据用户Id列表，获取所有员工
        /// </summary>
        /// <param name="userIds">用户Id列表</param>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetUsersByIds(List<string> userIds)
        {
            var jsonData = SerializeHelper.ToJson(userIds);
            ServiceResult<List<UserDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<UserDTO>>>(
                ServiceName + ".GetUsersByIds",
                AccoutApiServerUrl + "AccountApi/GetUsersByIds",
                ApplicationConstant.AccScope, 
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取所有组织的下的所有员工
        /// </summary>
        /// <param name="orgIds">所有组织的Id列表</param>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetUsersByOrgIds(List<int> orgIds)
        {
            var jsonData = SerializeHelper.ToJson(orgIds);
            ServiceResult<List<UserDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<UserDTO>>>(
                ServiceName + ".GetUsersByOrgIds",
                AccoutApiServerUrl + "AccountApi/GetUsersByOrgIds",
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取所有角色的下的所有员工
        /// </summary>
        /// <param name="roleIds">所有角色的Id列表</param>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetUsersByRoleIds(List<string> roleIds)
        {
            var jsonData = SerializeHelper.ToJson(roleIds);
            ServiceResult<List<UserDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<UserDTO>>>(
                ServiceName + ".GetUsersByRoleIds",
                AccoutApiServerUrl + "AccountApi/GetUsersByRoleIds",
                ApplicationConstant.AccScope, 
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 根据用户名列表，查询的所有员工
        /// </summary>
        /// <param name="names">用户名列表</param>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetUsersByNames(List<string> names)
        {
            var jsonData = SerializeHelper.ToJson(names);
            ServiceResult<List<UserDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<UserDTO>>>(
                ServiceName + ".GetUsersByNames",
                AccoutApiServerUrl + "AccountApi/GetUsersByNames",
                ApplicationConstant.AccScope, 
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 根据用户邮箱列表，查询的所有员工
        /// </summary>
        /// <param name="emails">用户邮箱列表</param>
        /// <returns></returns>
        public async Task<List<UserDTO>> GetUsersByEmails(List<string> emails)
        {
            var jsonData = SerializeHelper.ToJson(emails);
            ServiceResult<List<UserDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<UserDTO>>>(
                ServiceName + ".GetUsersByEmails",
                AccoutApiServerUrl + "AccountApi/GetUsersByEmails",
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取该用户所属的所有组织信息
        /// </summary>
        /// <param name="userId">用户Guid</param>
        /// <returns></returns>
        public async Task<UserDTO> GetUserWithOrganizationsByUserId(string userId)
        {
            ServiceResult<UserDTO> result = null;
            await WebSendGetAsync<ServiceResult<UserDTO>>(
                ServiceName + ".GetUserWithOrganizationsByUserId",
                AccoutApiServerUrl + "AccountApi/GetUserWithOrganizationsByUserId?userId=" + userId,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<UserDTO>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        /// <summary>
        /// 获取用户的简单数据（包含：所属的组织架构列表，不包含：系统用参数--邮箱是否确定、加密密码等等）
        /// </summary>
        /// <param name="userId">用户Guid</param>
        /// <returns></returns>
        public async Task<UserSimpleDTO> GetSimpleUserWithOrganizationsByUserId(string userId)
        {
            ServiceResult<UserSimpleDTO> result = null;
            await WebSendGetAsync<ServiceResult<UserSimpleDTO>>(
                ServiceName + ".GetSimpleUserWithOrganizationsByUserId",
                AccoutApiServerUrl + "AccountApi/GetSimpleUserWithOrganizationsByUserId?userId=" + userId,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<UserSimpleDTO>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
            {
                return result.Result;
            }

            return null;
        }
        
        #endregion

        #region 更改用户信息
        /// <summary>
        /// 修改租户的系统管理员的默认密码：123456
        /// </summary>
        /// <param name="password"></param>
        /// <param name="adminEmail"></param>
        /// <param name="adminPhone"></param>
        /// <returns></returns>
        public async Task<ServiceResult<bool>> ChangeAdminRawInfo(string password, string adminEmail, string adminPhone)
        {
            ServiceResult<bool> result = null;
            await WebSendGetAsync<ServiceResult<bool>>(
                ServiceName + ".ChangeAdminRawInfo",
                AccoutApiServerUrl + "AccountApi/ChangeAdminRawInfoAsync?password=" + password + "&adminEmail=" + adminEmail + "&adminPhone=" + adminPhone,
                ApplicationConstant.AccScope, 
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            return result;
        }

        /// <summary>
        /// 修改登录用户的密码
        /// </summary>
        /// <param name="userId">登录用户的Id</param>
        /// <param name="currentPassword">登录用户的原密码</param>
        /// <param name="newPassword">登录用户的新密码</param>
        /// <returns></returns>
        public async Task<ServiceResult<bool>> ChangePasswordAsync(string userId, string currentPassword, string newPassword)
        {
            ServiceResult<bool> result = null;
            await WebSendGetAsync<ServiceResult<bool>>(
                ServiceName + ".ChangePasswordAsync",
                AccoutApiServerUrl + "AccountApi/ChangePasswordAsync?userId=" + userId + "&currentPassword=" + currentPassword + "&newPassword=" + newPassword,
                ApplicationConstant.AccScope,
                DefaultContentType,
                null,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            return result;
        }
        /// <summary>
        /// 验证登录用户是否存在
        /// </summary>
        /// <param name="currentUserId">登录用户的Id</param>
        /// <returns></returns>
        public async Task<bool> CheckChangedPwd(string currentUserId)
        {
            ServiceResult<bool> result = null;
            await WebSendGetAsync<ServiceResult<bool>>(
                ServiceName + ".CheckChangedPwd",
                AccoutApiServerUrl + "AccountApi/CheckChangedPwd?currentUserId=" + currentUserId,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            return result.success && result.Result;
        }
        /// <summary>
        /// 修改登录用户的邮箱及密码
        /// </summary>
        /// <param name="userId">登录用户的Id</param>
        /// <param name="email">登录用户的邮箱</param>
        /// <param name="phone">登录用户的密码</param>
        /// <returns></returns>
        public async Task<ServiceResult<bool>> ChangeMailPhoneAsync(string userId, string email, string phone)
        {
            ServiceResult<bool> result = null;
            await WebSendGetAsync<ServiceResult<bool>>(
                ServiceName + ".ChangeMailPhoneAsync",
                AccoutApiServerUrl + "AccountApi/ChangeMailPhoneAsync?userId=" + userId + "&email=" + email + "&phone=" + phone,
                ApplicationConstant.AccScope,
                DefaultContentType,
                null,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<bool>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            return result;
        }
        #endregion
        #endregion

        #region 组织架构及其员工
        /// <summary>
        /// 获取所有的部门信息
        /// </summary>
        /// <param name="appid">应用Id</param>
        /// <returns></returns>
        public async Task<List<OrganizationDTO>> GetRootOrganizations()
        {
            ServiceResult<List<OrganizationDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<OrganizationDTO>>>(
                ServiceName + ".GetRootOrganizations",
                AccoutApiServerUrl + "AccountApi/GetRootOrganizations",
                ApplicationConstant.AccScope, 
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new List<OrganizationDTO>();
        }

        /// <summary>
        /// 根据组织Id列表，获取所有的部门信息（部门列表，包含其下属所有的部门信息：递归查询）
        /// </summary>
        /// <param name="appid">可为：Guid.Empty</param>
        /// <param name="ids">组织Id列表</param>
        /// <returns></returns>
        public async Task<List<OrganizationDTO>> GetNestOrganizationsByIds(List<int> ids)
        {
            var jsonData = SerializeHelper.ToJson(ids);
            ServiceResult<List<OrganizationDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<OrganizationDTO>>>(
                ServiceName + ".GetNestOrganizationsByIds",
                AccoutApiServerUrl + "AccountApi/GetNestOrganizationsByIds",
                ApplicationConstant.AccScope, 
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new List<OrganizationDTO>();
        }
        /// <summary>
        /// 获取所有的部门信息及下属员工
        /// </summary>
        /// <param name="appid">应用Id</param>
        /// <returns></returns>
        public async Task<List<OrganizationDTO>> GetOrganizationsWithUsers()
        {
            ServiceResult<List<OrganizationDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<OrganizationDTO>>>(
                ServiceName + ".GetOrganizationsWithUsers",
                AccoutApiServerUrl + "AccountApi/GetOrganizationsWithUsers",
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new List<OrganizationDTO>();
        }
        /// <summary>
        /// 获取所有的角色下的部门及下属员工
        /// </summary>
        /// <param name="appid">应用Id</param>
        /// <param name="roleIds">角色Id列表</param>
        /// <returns></returns>
        public async Task<List<OrganizationDTO>> GetOrganizationsWithUsersByRoleIds(List<string> roleIds)
        {
            var jsonData = SerializeHelper.ToJson(roleIds);
            ServiceResult<List<OrganizationDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<OrganizationDTO>>>(
                ServiceName + ".GetOrganizationsWithUsersByRoleIds",
                AccoutApiServerUrl + "AccountApi/GetOrganizationsWithUsersByRoleIds",
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new List<OrganizationDTO>();
        }
        /// <summary>
        /// 根据用户Id，获取其所属部门及下属员工
        /// </summary>
        /// <param name="userId">用户Id</param>
        /// <returns></returns>
        public async Task<List<OrganizationDTO>> GetOrganizationsWithUsersByUserId(string userId)
        {
            ServiceResult<List<OrganizationDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<OrganizationDTO>>>(
                ServiceName + ".GetOrganizationsWithUsersByUserId",
                AccoutApiServerUrl + "AccountApi/GetOrganizationsWithUsersByUserId?userId=" + userId,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new List<OrganizationDTO>();
        }
        /// <summary>
        /// 获取角色及部门下的所有部门及其下属员工
        /// </summary>
        /// <param name="searchModel">
        /// roleIds：角色Id列表
        /// depIds：部门Id列表
        /// </param>
        /// <returns></returns>
        public async Task<List<OrganizationDTO>> GetOrgsWithUsersByRoleIdsAndOrgids(RootOrgWithUsersSearchDTO searchModel)
        {
            var jsonData = SerializeHelper.ToJson(searchModel);
            ServiceResult<List<OrganizationDTO>> result = null;
            await WebSendPostAsync<ServiceResult<List<OrganizationDTO>>>(
                ServiceName + ".GetOrgsWithUsersByRoleIdsAndOrgids",
                AccoutApiServerUrl + "AccountApi/GetOrgsWithUsersByRoleIdsAndOrgids",
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new List<OrganizationDTO>();
        }
        /// <summary>
        /// 获取分页的部门及其下属员工数据
        /// </summary>
        /// <param name="searchModel"></param>
        /// <returns></returns>
        public async Task<PaginatedBaseDTO<OrganizationDTO>> GetPaginatedOrganizationsWithUsersByFilter(RootOrgWithUsersPaginatedSearchDTO searchModel)
        {
            var jsonData = SerializeHelper.ToJson(searchModel);
            ServiceResult<PaginatedBaseDTO<OrganizationDTO>> result = null;
            await WebSendPostAsync<ServiceResult<PaginatedBaseDTO<OrganizationDTO>>>(
                ServiceName + ".GetPaginatedRootOrganizationsWithUsersByFilter",
                AccoutApiServerUrl + "AccountApi/GetPaginatedRootOrganizationsWithUsersByFilter",
                ApplicationConstant.AccScope,
                jsonData,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<PaginatedBaseDTO<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new PaginatedBaseDTO<OrganizationDTO>();
        }

        /// <summary>
        /// 根据部门Fid获取下面人员信息
        /// </summary>
        /// <param name="orgfid"></param>
        /// <param name="appid"></param>
        /// <returns></returns>
        public async Task<List<OrganizationDTO>> GetUserByorgFid(Guid orgfid)
        {
            ServiceResult<List<OrganizationDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<OrganizationDTO>>>(
                ServiceName + ".GetUserByorgFid",
                AccoutApiServerUrl + "AccountApi/GetUserByorgFid?orgfid=" + orgfid,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new List<OrganizationDTO>();

        }
        /// <summary>
        /// 获取所有的部门信息(非树状)list类型
        /// </summary>
        /// <returns></returns>
        public async Task<List<OrganizationDTO>> GetAllOrganization()
        {
            ServiceResult<List<OrganizationDTO>> result = null;
            await WebSendGetAsync<ServiceResult<List<OrganizationDTO>>>(
                ServiceName + ".GetAllOrganization",
                AccoutApiServerUrl + "AccountApi/GetAllOrganization",
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<List<OrganizationDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;
            return new List<OrganizationDTO>();
        }
        /// <summary>
        /// 获取所有的员工
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="searchUserName"></param>
        /// <param name="searchEmail"></param>
        /// <param name="searchPhone"></param>
        /// <param name="searchName"></param>
        /// <param name="departmentId"></param>
        /// <param name="statType"></param>
        /// <param name="positionLevel"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public async Task<PaginatedBaseDTO<UserDTO>> GetAllUser(
            int page = 1, int rows = 10, string searchUserName = "", string searchEmail = "", 
            string searchPhone = "", string searchName = "", string departmentId = "", string statType = "",
            PositionLevel? positionLevel = null, string sort = "CreateDate", string order = "desc")
        {
            ServiceResult<PaginatedBaseDTO<UserDTO>> result = null;
            await WebSendGetAsync<ServiceResult<PaginatedBaseDTO<UserDTO>>>(
                ServiceName + ".GetAllUser",
                AccoutApiServerUrl + "AccountApi/GetAllUser?page=" + page + "&rows=" + rows + "&searchUserName=" + searchUserName + "&searchEmail=" + searchEmail + "&searchPhone=" + searchPhone + "&searchName=" + searchName + "&departmentId=" + departmentId + "&statType=" + statType + "&positionLevel=" + null + "&sort=" + sort + "&order=" + order,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<PaginatedBaseDTO<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;
            return new PaginatedBaseDTO<UserDTO>();
        }

        /// <summary>
        /// 根据部门范围获取其下人员
        /// </summary>
        /// <param name="startDeptFid"></param>
        /// <param name="endDeptFid"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public async Task<PaginatedBaseDTO<UserDTO>> GetUsersByOrgRange(Guid startDeptFid, Guid endDeptFid, int page, int rows)
        {
            ServiceResult<PaginatedBaseDTO<UserDTO>> result = null;
            await WebSendGetAsync<ServiceResult<PaginatedBaseDTO<UserDTO>>>(
                ServiceName + ".GetUsersByOrgRange",
                AccoutApiServerUrl + "AccountApi/GetUsersByOrgRange?startDeptFid=" + startDeptFid + "&endDeptFid=" + endDeptFid + "&page=" + page + "&rows=" + rows,
                ApplicationConstant.AccScope, 
                callback =>
                {
                    result = callback;
                },
                (httpStatusCode, errorMessage) =>
                {
                    result = new ServiceResult<PaginatedBaseDTO<UserDTO>>(ServiceResultType.Error, httpStatusCode, errorMessage);
                },
                true);

            if (result.success && result.Result != null)
                return result.Result;

            return new PaginatedBaseDTO<UserDTO>();
            
        }


        #endregion

        /// <summary>
        /// 获取企业的UKey认证数据
        /// </summary>
        /// <param name="memberId"></param>
        /// <returns></returns>
        public ServiceResult<UkeyAuthenticationDTO> GetUkeyAuthenticationByMemberId(string memberId)
        {
            ServiceResult<UkeyAuthenticationDTO> result = null;
            WebSendGet<ServiceResult<UkeyAuthenticationDTO>>(
                ServiceName + ".GetUkeyAuthenticationByMemberId",
                AccoutApiServerUrl + "Account/GetUkeyAuthenticationByMemberId?memberId=" + memberId,
                ApplicationConstant.AccScope,
                callback =>
                {
                    result = callback;
                },
                 (httpStatusCode, errorMessage) =>
                 {
                     result = new ServiceResult<UkeyAuthenticationDTO>(ServiceResultType.Error, errorMessage);
                 },
                true);
            return result;
        }
    }
}
